<?php
include_once "db.php";
include_once "checksession.php"; 
?>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
    <title>
      Online College Helpdesk - Statistics
    </title>
    <link href="meta-style.css" rel="stylesheet" type="text/css" />
	
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
	<script src="js/highcharts.js" type="text/javascript"></script>
</head>
<body>
<?php 
include_once "topnav.php";
	$startDate = new DateTime();
	$endDate = new DateTime();

	if(empty($_GET))
	{
		//no date specified, defaults to 20 weeks ago till today
		//$startDate->modify('20 weeks ago');
		date_modify($startDate, '20 weeks ago');
	}
	else 
	{
		$startDate->setDate($_GET["sy"], $_GET["sm"], $_GET["sd"]);
		$endDate->setDate($_GET["ey"], $_GET["em"], $_GET["ed"]);
	}
?>
<div align="center">
	<form id="date" method="get" action="weeklygraph.php">
		From 00:00 on
		<select id="startMonth" name="sm">
			<?php
			for ($i = 1; $i <= 12; $i++)		//so months are 1-based index like the numerical date
			{
				echo "<option value=\"$i\" ";
				if ($i == (int)$startDate->format('m'))
					echo "selected ";			//displays the query dates in the selecttion
				echo ">";
				echo $i;
				echo "</option>";
			}
			?>
		</select>
		<select id="startDay" name="sd">
			<?php		
			for ($i = 1; $i <= 31; $i++)		//every month will have 31 days. crazy roman calendar...
			{
				echo "<option value=\"$i\" "; 
				if ($i == (int)$startDate->format('d'))
					echo "selected ";
				echo ">";
				echo $i;
				echo "</option>";
			}
			?>
		</select>
		<select id="startYear" name="sy">
			<?php
			for ($i = 2007; $i <= date("Y"); $i++)	//it will automagically update for 2012 and 2013 etc. (if the world doesnt end)
			{
				echo "<option value=\"$i\" ";
				if ($i == (int)$startDate->format('Y'))
					echo "selected ";
				echo ">";
				echo $i;
				echo "</option>";
			}
			?>
		</select>
		to 23:59 on
		<select id="endMonth" name="em">
			<?php
			for ($i = 1; $i <= 12; $i++)		//so months are 1-based index like the numerical date
			{
				echo "<option value=\"$i\" ";
				if ($i == (int)$endDate->format('m'))
					echo "selected ";			//displays the query dates in the selecttion
				echo ">";
				echo $i;
				echo "</option>";
			}
			?>
		</select>
		<select id="endDay" name="ed">";
			<?php
			for ($i = 1; $i <= 31; $i++)		//every month will have 31 days. crazy roman calendar...
			{
				echo "<option value=\"$i\" "; 
				if ($i == (int)$endDate->format('d'))
					echo "selected ";
				echo ">";
				echo $i;
				echo "</option>";
			}
			?>
		</select>
		<select id="endYear" name="ey">
			<?php
			for ($i = 2007; $i <= date("Y"); $i++)	//it will automagically update for 2012 and 2013 etc. (if the world doesnt end)
			{
				echo "<option value=\"$i\" ";
				if ($i == (int)$endDate->format('Y'))
					echo "selected ";
				echo ">";
				echo $i;
				echo "</option>";
			}
			?>
		</select>
		<input type="submit" name="Submit" value="Submit" />
	</form>
</div>
<?php		
	//fix dates to weekends
	$startDate->modify("Sunday last week");
	$endDate->modify("Saturday");
	
	echo $startDate->format(DATE_RSS);
	echo "<br/>";
	echo $endDate->format(DATE_RSS);

	//make an array to contain the start and end arrays
	$weeks = array();	//an array[n] of array[2] of string
	$weekCounter = 0;
	while ($startDate < $endDate)	//use getTimestamp for a numerical comparison of datetimes
	{
		$weeks[$weekCounter] = array();	//from midnite sunday till midnite - 00:00:01 on saturday
		$weeks[$weekCounter]['start'] = $startDate->format("Y-m-d G:i:s");
		$startDate->modify("saturday 23:59:00");
		$weeks[$weekCounter]['end'] = $startDate->format("Y-m-d G:i:s");
		$startDate->modify("sunday");		//midnite sunday
		$weekCounter += 1;
	}
	/*
	*we have the array of start and end dates for each week
	*next, we will run the queries for each week in the array,
	*putting the results in hidden divs as we go
	*/

	?>
<div id="chart"></div>
<div id="data">
	<table id="results" border="1px">
		<tr>
			<td>
				Week:
			</td>
			<td>
				Total calls
			</td>
		</tr>
		<?php
		
		//total calls
		$weekCounter = 0;	//zeroth week of specified range
		foreach($weeks as $week)
		{
		?>
		<tr>
			<td name="start">
				<?php echo $week['start']; ?>
			</td>
			<td name="end">
				<?php echo $week['end']; ?>
			</td>
			<td name="total">
				<?php
					$result = mysql_query("select count(*) from Calls WHERE Calls.request_date > '{$week['start']}' AND Calls.request_date < '{$week['end']}'");
					echo mysql_result($result, 0); 
				?>
			</td>
		</tr>
		<?php
		}//end foreach
		?>
	</table>
</div>
<script type="text/javascript">
function makeChart()
{
	//organize data
	var xData = [];
	var yData = [];
	var weekStarts = document.getElementsByName("start");
	var weekCounts = document.getElementsByName("total");
	for (var i = 0; i < weekStarts.length; i++)
	{
		xData[i] = jQuery.trim(weekStarts[i].innerHTML).substr(0, 10);		//use substr to display dates but not times
		yData[i] = parseInt(weekCounts[i].innerHTML);
	}

	//create the chart
	var chart = new Highcharts.Chart({
	chart: {
		renderTo: 'chart',
		defaultSeriesType: 'spline',
		plotBackgroundColor: null,
		plotBorderWidth: null,
		plotShadow: false,
		height: 600
	},
	title: {
		text: 'Total tickets per week'
	},
	xAxis: {
		categories: xData
	},
	tooltip: {
		formatter: function() {
			return 'Week of:<br/><b>'+ this.x +'</b><br/>'+ this.y + ' tickets';
		}
	},
	plotOptions: {
		pie: {
			allowPointSelect: true,
			cursor: 'pointer',
			dataLabels: {
				enabled: false
			},
			showInLegend: true
		}
	},
	series: [{
		name: 'problem types',
		data: yData
		}]
	});
}

window.onload = makeChart();
</script>
</body>
</html>
